<template>
    <view class="page">
        <hm-filter :menuTop="0" :filterData="menuList" :defaultSelected="defaultSelected" :updateMenuName="true" @confirm="confirm" dataFormat="Object"></hm-filter>
        <view class="content">
            <house-list :house-list="houseList"></house-list>
            <!--<u-loadmore :status="loadStatus" />-->
            <view class="safe-area-inset-bottom"></view>
        </view>
    </view>
</template>

<script>
    import HmFilter from "../../../components/hm-filter";
    import HouseList from './components/HouseList';
    export default {
        name: "apartment",
        components: {HmFilter,HouseList},
        data(){
            return{
                loadStatus:'loadmore',
                page:{
                    current:1,
                    count:'',
                    size: 10,
                },
                houseList:[],
                defaultSelected: [],
                menuList:[
                    {
                        "name":'区域',
                        "type": 'hierarchy-column',
                        "submenu": [
                            {
                                "name": '附近',
                                "value": "raidus",
                                "submenu": [{
                                    "name": "不限",
                                    "value": ""
                                }, {
                                    "name": "1000m内",
                                    "value": "1000"
                                }, {
                                    "name": "3000m内",
                                    "value": "3000"
                                }, {
                                    "name": "5000m内",
                                    "value": "5000"
                                }]
                            },
                            {
                                "name": '区域',
                                "value": "area",
                                "submenu": [{
                                    "name": "不限",
                                    "value": ""
                                }]
                            },
                            {
                                "name": '地铁',
                                "value": "ditie",
                                "submenu": [{
                                    "name": "不限",
                                    "value": ""
                                }, {
                                    "name": "1号线",
                                    "value": ""
                                }, {
                                    "name": "2号线",
                                    "value": ""
                                }, {
                                    "name": "3号线",
                                    "value": ""
                                }, {
                                    "name": "5号线",
                                    "value": ""
                                }]
                            },
                        ]
                    },
                    {
                        "name":'租金',
                        "type": 'radio',
                        "submenu": [{
                            "name": "请选择租金",
                            "submenu": [{
                                "name": "不限",
                                "value": ""
                            },{
                                "name": "1000以下",
                                "value": "1000"
                            },{
                                "name": "1000-1500",
                                "value": "1000-1500"
                            },{
                                "name": "1500-2000",
                                "value": "1500-2000"
                            },{
                                "name": "2000-3000",
                                "value": "2000-3000"
                            },{
                                "name": "3000-5000",
                                "value": "3000-5000"
                            },{
                                "name": "5000以上",
                                "value": "5000"
                            }]
                        }]
                    },
                    {
                        "name":'户型筛选',
                        "type": 'radio',
                        "submenu": [{
                            "name": "户型",
                            "submenu": [{
                                "name": "一室户",
                                "value": "1"
                            },{
                                "name": "两室户",
                                "value": "2"
                            },{
                                "name": "三室户",
                                "value": "3"
                            },{
                                "name": "四室户",
                                "value": "4"
                            },{
                                "name": "更大户型",
                                "value": "bigger"
                            },]
                        }]
                    },
                    {
                        "name":'筛选',
                        "type": 'filter',
                        "submenu": [
                            {
                                "name": "平台保障",
                                "submenu": []
                            },
                            {
                                "name": "付款方式",
                                "submenu": []
                            },
                            {
                                "name": "房源特色",
                                "submenu": []
                            },
                        ]
                    },
                ],
                areaList:[],
                ditieList:[],
                filter:{
                    type: 1
                },
            }
        },
        // type=1		#房间类型 1 精品公寓 2分散式合租 3 分散式整租
        // &longitude	#经度
        // &latitude		#纬度
        // &raidus		#米  100
        // &price		#价格 100 或 100-400
        // &special		#特色id id,id
        // &order		#排序 desc asc 默认asc
        onLoad: function() {
            // 合肥各区
            uni.$u.http.get('/api.php/index/area_list?type=10').then(res => {
                this.areaList = res;
                for (let i = 0; i < res.length; i++) {
                    this.menuList[0].submenu[1].submenu.push({
                        'name': res[i].AREA_NAME,
                        'value': res[i].AREA_ID
                    });
                }
            });

            //保障选项
            uni.$u.http.get('/api.php/UserClinet/index_get_guarantee').then(res => {
                for (let i = 0; i < res.length; i++) {
                    this.menuList[3].submenu[0].submenu.push({
                        'name': res[i].name,
                        'value': res[i].id
                    });
                }
            });

            //支付选项
            uni.$u.http.get('/api.php/UserClinet/index_get_pay').then(res => {
                for (let i = 0; i < res.length; i++) {
                    this.menuList[3].submenu[1].submenu.push({
                        'name': res[i].name,
                        'value': res[i].id
                    });
                }
            });

            //特色筛选项
            uni.$u.http.get('/api.php/UserClinet/index_get_special').then(res => {
                for (let i = 0; i < res.length; i++) {
                    this.menuList[3].submenu[2].submenu.push({
                        'name': res[i].name,
                        'value': res[i].id
                    });
                }
            });

            // 获取地铁
            uni.$u.http.get('/api.php/UserClinet/getDiTie' + uni.$u.queryParams({u_id: uni.getStorageSync('u_id') ?? 1})).then(res => {
                let array = [];
                for (let i = 0; i < res.length; i++) {
                    let parent = {};
                    parent.name = res[i].Stationname;
                    parent.value = i;
                    parent.submenu = [];
                    for (let j = 0; j < res[i].son.length; j++) {
                        parent.submenu.push({
                            name:res[i].son[j].Stationname,
                            value:j,
                        })
                    }
                    array.push(parent);
                }
                this.menuList[0].submenu[2].submenu = array;
                this.ditieList = res;
            });

            // 获取列表
            // this.getHouseList(this.filter).then(function (res) {
            //     this.houseList = res;
            // });
            this.getHouseList(this.filter);
        },
        onReachBottom() {
            // 页面滚动到底

            // let _this = this;
            // if (this.page.count > this.page.current) {
            //     this.page.current++;
            //     this.loadStatus = "loading";
            //     this.getHouseList().then(function (res) {
            //         _this.houseList.push(...res);
            //         _this.loadStatus = "loadmore";
            //     });
            // } else {
            //     this.loadStatus = "nomore";
            // }
        },
        methods:{
            //接收菜单结果
            confirm(e) {
                if (e.value[0][0] === 'area') {
                    for (let i = 0; i < this.areaList.length; i++) {
                        if (this.areaList[i].AREA_ID === e.value[0][1]){
                            this.filter.latitude = this.areaList[i].LAT;
                            this.filter.longitude = this.areaList[i].LNG;
                        }
                    }
                }

                if (e.value[0][0] === 'raidus') {
                    this.filter.raidus = e.value[0][1];
                }

                if (e.value[0][0] === 'ditie') {
                    this.filter.latitude = this.ditieList[e.value[0][1]].son[e.value[0][2]].LAT;
                    this.filter.longitude = this.ditieList[e.value[0][1]].son[e.value[0][2]].LNG;
                }

                if (e.value[1][0].length > 0) {
                    this.filter.price = e.value[1][0][0];
                }

                if (e.value[2][0].length > 0) {
                    this.filter.lease_type = e.value[2][0][0];
                }

                //保障选项
                if (e.value[3][0].length > 0 ){
                    this.filter.guarantee = e.value[3][0].join();
                }
                //支付选项
                if (e.value[3][1].length > 0 ){
                    this.filter.pay = e.value[3][1].join();
                }

                if (e.value[3][2].length > 0 ){
                    this.filter.special = e.value[3][2].join();
                }

                // let _this = this;
                // console.log(this.filter);
                // _this.page.current = 1;
                // // todo 组装params
                // let params = {};
                // _this.loadStatus = "loading";
                // _this.getHouseList(params).then(function (res) {
                //     _this.houseList = res;
                //     _this.loadStatus = _this.houseList.length < _this.page.size ? "nomore" : "loadmore";
                // });
                // this.getHouseList(this.filter).then(function (res) {
                //     this.houseList = res;
                // })

                this.getHouseList(this.filter);
            },

            getHouseList(params){
                // params.current = this.page.current;
                // params.size = this.page.size;
                // return new Promise(function (resolve, reject) {
                //     uni.showLoading({title: '正在加载'});
                //     uni.$u.http.get('/api.php/UserClinet/index' + uni.$u.queryParams(params),).then(res => {
                //         // todo 这里只处理页码 然后将数据吐给下个调用的方法，方便push 和直接赋值
                //         uni.hideLoading();
                //         resolve(res.data);
                //     }).catch((err) => {
                //         reject(err);
                //     });
                // });

                uni.showLoading({title: '正在加载'});
                uni.$u.http.get('/api.php/UserClinet/index' + uni.$u.queryParams(params),).then(res => {
                    uni.hideLoading();
                    this.houseList = res.data;
                });
            },
        }
    }
</script>

<style scoped>
    .page {
        width: 100%;
        min-height: 100vh;
        /*background-color: RGBA(245, 245, 245, 1);*/
        background-color: #ffffff;
    }
    .content{
        padding: 100rpx 16rpx 16rpx 16rpx;
    }
</style>